<?php

/**
 * 工具 (upload) Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: tool.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class toolAction extends Action
{

    private $tmp_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->tmp_model = Tmac::model('article');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        $this->redirect('正在转向首页!', '/');
        exit;
    }

    public function upload()
    {
        $filename = !empty($_GET['filename']) ? $_GET['filename'] : 'thumb_upload';
        $action = !empty($_GET['action']) ? $_GET['action'] : 'article';
        $type = $this->getParam('type');

        switch ($action)
        {
            case 'article':
                $data_dir = WEB_ROOT . 'Public/uploadfiles/product/';
                $dir = '';
                break;

            case 'download':
                $data_dir = WEB_ROOT . 'Public/uploadfiles/download/';
                $dir = '';
                break;

            case 'link':
                $data_dir = WEB_ROOT . 'Public/uploadfiles/';
                $dir = 'flink';
                break;

            case 'adflash':
                $data_dir = WEB_ROOT . 'Public/uploadfiles/';
                $dir = 'adflash';
                break;
        }

        if ($type == 'file') {
            $typefile = $type;
        } else {
            $typefile = '';
        }
        /**
            $debug = Debug::getInstance();
            $debug->debugAjax($_FILES);
        */
        $image = new Image($data_dir, $thumb_dir = '');
        $upload_image = $image->upload_image($filename, 0, $dir, '', $typefile);
        $error = '';
        if ($upload_image === false) {
//            $rs = $image->error_msg();
            $error = $image->error_msg();
            $url = '';
        } else {
            $url = $upload_image;
        }
        echo "{";
        echo "error: '" . $error . "',\n";
        echo "url: '" . $url . "'\n";
        echo "}";
    }

    public function Captcha()
    {
        $image = imagecreatetruecolor(58, 22);
        $color_Background = imagecolorallocate($image, 255, 255, 255);
        imagefill($image, 0, 0, $color_Background);
        $key = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $string = null;
        $char_X = 6;
        $char_Y = 0;
        for ($i = 0; $i < 4; $i++) {
            $char_Y = mt_rand(0, 5);
            $char = $key[mt_rand(0, 9)];
            $string .= $char;
            $color_Char = imagecolorallocate($image, mt_rand(0, 230), mt_rand(0, 230), mt_rand(0, 230));
            imagechar($image, 5, $char_X, $char_Y, $char, $color_Char);
            $char_X = $char_X + mt_rand(8, 15);
        }
        $line_X1 = 0;
        $line_Y1 = 0;
        $line_X2 = 0;
        $line_Y2 = 0;
        for ($i = 0; $i < mt_rand(0, 64); $i++) {
            $line_X1 = mt_rand(0, 58);
            $line_Y1 = mt_rand(0, 22);
            $line_X2 = mt_rand(0, 58);
            $line_Y2 = mt_rand(0, 22);
            $line_X1 = $line_X1;
            $line_Y1 = $line_Y1;
            $line_X2 = $line_X1 + mt_rand(1, 8);
            $line_Y2 = $line_Y1 + mt_rand(1, 8);
            $color_Line = imagecolorallocate($image, mt_rand(0, 230), mt_rand(0, 230), mt_rand(0, 230));
            imageline($image, $line_X1, $line_Y1, $line_X2, $line_Y2, $color_Line);
        }
        $_SESSION['valid'] = md5($string);
        header('Content-Type: image/jpeg');
        imagepng($image);
        imagedestroy($image);
    }

}